/*
** Copyright (c) 2008 Mobile GCalendarManager
**
** Permission is hereby granted, free of charge, to any person
** obtaining a copy of this software and associated documentation
** files (the "Software"), to deal in the Software without
** restriction, including without limitation the rights to use,
** copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following
** conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
** OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 *
 */
package au.edu.mq.comp.itec800.mgc.ui.cli.commands;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.log4j.Logger;

import au.edu.mq.comp.itec800.mgc.MGC;
import au.edu.mq.comp.itec800.mgc.ui.cli.ICommand;
import au.edu.mq.comp.itec800.mgc.util.CommandUtils;
import au.edu.mq.comp.itec800.mgc.util.MGCUtils;

import com.google.gdata.client.Query;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.youtube.CommentEntry;
import com.google.gdata.data.youtube.CommentFeed;
import com.google.gdata.util.ServiceException;

/**
 * Comment-context management class.
 *
 * @author Laurent Malvert [laurent.malvert@students.mq.edu.au]
 * @author Andrey Chislenko [andrey.chislenko@students.mq.edu.au]
 */
public enum CommentCommand implements ICommand
{
  add
  {
    @Override
    public boolean  execute(final String ... args)
    {
      return (false);
    }
  },
  edit
  {
    @Override
    public boolean  execute(final String ... args)
    {
      return (false);
    }
  },
  help
  {
    @Override
    public boolean  execute(final String ... args)
    {
      return (false);
    }
  },
  list
  {
    @Override
    public boolean  execute(final String ... args)
    {
      return (false);
    }
  },
  noop
  {
    @Override
    public boolean  execute(final String ... args)
    {
      return (true);  //  does nothing but does it well
    }
  },
  remove
  {
    @Override
    public boolean  execute(final String ... args)
    {
      return (false);
    }
  },
  view
  {
    @Override
    public boolean execute(final String... args)
    {
      boolean succeeded = true;
      String eventID = args[0];
      URL feedURL = null;
      try
      {
        feedURL = new URL(eventID + "/comments");
      } catch (MalformedURLException e)
      {
        logger.error(e.getMessage(), e);
        succeeded = false;
      }
      Query query = new Query(feedURL);
      CalendarService service = MGC.getInstance().getActiveAccount()
          .getService();
      CommentFeed feed = null;
      try
      {
        feed = service.query(query, CommentFeed.class);
      } catch (IOException e)
      {
        logger.error(e.getMessage(), e);
        succeeded = false;
      } catch (ServiceException e)
      {
        logger.error(e.getMessage(), e);
        succeeded = false;
      }
      final StringBuffer view = new StringBuffer(512);
      if (feed.getEntries().size() > 0)
      {
        CommentEntry entry = feed.getEntries().get(0);
        view.append(entry.getTextContent().getContent().getPlainText());
      }
      MGCUtils.getConsole().getIO().output(view.toString());
      return (succeeded);
    }
  };

  public boolean  execute(final String ... args)
  {
    return (false);
  }

  public boolean  needsAuthentication()
  {
    return (false);
  }

  public ICommand         getMaster()
  {
    return (null);
  }

  public boolean          isAlias()
  {
    return (false);
  }

  public ICommand         getTopMaster()
  {
    return (CommandUtils.getTopMaster(this));
  }

  public String   help()
  {
    return ("");
  }
  private static final Logger               logger    = Logger.getLogger(MainCommand.class);
}
